package com.example.manage.controller;

import com.example.manage.bean.OwnerCarBean;
import com.example.manage.bean.OwnerCarBean;
import com.example.manage.bean.ResultBean;
import com.example.manage.mapper.OwnerCarMapper;
import com.example.manage.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ownerCar")
public class OwnerCarController {

    @Autowired
    OwnerCarMapper ownerCarMapper;

    @RequestMapping("/addOwnerCar")
    @ResponseBody
    public ResultBean addOwnerCar(@RequestBody OwnerCarBean ownerCarBean) {
        List<OwnerCarBean> beans = ownerCarMapper.getOwnerCarByNumber(ownerCarBean.getCarNumber());
        if (beans != null && beans.size()>0) {
          return   ResultUtil.getFailBean("该车牌号已存在，请修改后提交");
        }
        int count = ownerCarMapper.addOwnerCar(ownerCarBean);
        return ResultUtil.getResultBean(count, "添加失败");
    }

    @RequestMapping("/updateOwnerCar")
    @ResponseBody
    public ResultBean updateOwnerCar(@RequestBody OwnerCarBean ownerCarBean) {
        int count = ownerCarMapper.updateOwnerCar(ownerCarBean);
        return ResultUtil.getResultBean(count, "修改失败");
    }

    @RequestMapping("/deleteOwnerCar")
    @ResponseBody
    public ResultBean deleteOwnerCar(@RequestBody Map<String, Integer> params) {
        int count = ownerCarMapper.deleteOwnerCar(params.get("id"));
        return ResultUtil.getResultBean(count, "删除失败");
    }

    @RequestMapping("/getOwnerCar")
    @ResponseBody
    public ResultBean getOwnerCar() {
        List<OwnerCarBean> ownerCarBeans = ownerCarMapper.getOwnerCar();
        return ResultUtil.getSuccessBean(ownerCarBeans);
    }

    @RequestMapping("/getOwnerCarByNumber")
    @ResponseBody
    public ResultBean getOwnerCarByNumber(@RequestParam("number") String number) {
        List<OwnerCarBean> ownerCarBeans = ownerCarMapper.getOwnerCarByNumber(number);
        return ResultUtil.getSuccessBean(ownerCarBeans);
    }

    @RequestMapping("/getOwnerCarByOwnerId")
    @ResponseBody
    public ResultBean getOwnerCarByOwnerId(@RequestParam("ownerId") String ownerId) {
        List<OwnerCarBean> ownerCarBeans = ownerCarMapper.getOwnerCarByOwnerId(ownerId);
        return ResultUtil.getSuccessBean(ownerCarBeans);
    }

//    @RequestMapping("/getCarById")
//    @ResponseBody
//    public ResultBean getOwnerCarById(@RequestParam("ownerId") String ownerId) {
//        OwnerCarBean ownerCarBeans = ownerCarMapper.getCarById(ownerId);
//        return ResultUtil.getSuccessBean(ownerCarBeans);
//    }


}
